package org.git.movie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.git.movie.entity.FfPlayer;
import org.git.movie.entity.FfVod;
import org.git.movie.entity.FfVodWithBLOBs;
import org.git.movie.form.ListSearchForm;
import org.git.movie.mapper.FfVodMapper;
import org.git.movie.service.FfNavService;
import org.git.movie.service.FfPlayerService;
import org.git.movie.service.FfVodService;
import org.git.movie.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class FfVodServiceImpl  implements FfVodService {

    @Resource
    private FfVodMapper ffVodMapper;

    @Resource
    private FfPlayerService  ffPlayerService;
    @Resource
    private FfNavService ffNavService;

    @Override
    public PageDto<FfVodListDto> findRelationVodList(Integer vodId,int pageNo,int pageSize) {
        FfVod ffVod = ffVodMapper.findById(vodId);
        if(ffVod == null){
            PageDto rep =    PageDto.empty(pageNo,pageSize);

            return rep;
        }else{
            //主演
            String vodActor = ffVod.getVodActor();
            //导演
            String vodDirector =    ffVod.getVodDirector();
            //关联系列
            String vodSeries = ffVod.getVodSeries();


            FfVodRelationDto ffVodRelationDto = new FfVodRelationDto();
            ffVodRelationDto.setVodId(vodId);
            if(StringUtils.isNotEmpty(vodActor)){
                ffVodRelationDto.setVodActor(vodActor.split(",")[0]);
            }
            if(StringUtils.isNotEmpty(vodDirector)){
                ffVodRelationDto.setVodDirector(vodDirector.split(",")[0]);
            }
            if(StringUtils.isNotEmpty(vodSeries)){
                ffVodRelationDto.setVodSeries(vodSeries);
            }


            PageHelper.startPage(pageNo, pageSize, true);
            List<FfVod>  list = ffVodMapper.findRelationList(ffVodRelationDto);

            //用PageInfo对结果进行包装
            PageInfo page = new PageInfo(list);

            List<FfVodListDto> repList = new ArrayList<>();

            if(list != null && list.size() >= 1){
                for(FfVod vod : list){
                    FfVodListDto dto = transFfVod2FfVodListDto(vod);
                    dto.setVodId(vod.getVodId());

                    repList.add(dto);
                }
            }
            page.setList(repList);

            //自定义转换方法
            return   PageDto.transPageInfo(page);
        }

    }

    /**
     * 根据分类查询视频列表
     * @param listSearchForm
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageDto<FfVodListDto> findListVodList(ListSearchForm listSearchForm, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize, true);

        List<FfVod>  list =  ffVodMapper.findListVodList(listSearchForm);
        PageInfo page = new PageInfo(list);
        List<FfVodListDto> repList = new ArrayList<>();

        if(list != null && list.size() >= 1){
            for(FfVod vod : list){
                FfVodListDto dto = transFfVod2FfVodListDto(vod);
                dto.setVodId(vod.getVodId());

                repList.add(dto);
            }
            page.setList(repList);
        }
        return   PageDto.transPageInfo(page);

    }

    @Override
    public FfVodReadDto findbyid4dto(Integer videoId) {

        FfVodWithBLOBs ffVod = ffVodMapper.findAllById(videoId);
        if(ffVod == null){

            return null;
        }else{
            FfVodReadDto dto = this.findbyid4dto(ffVod,null,null);
            return dto;
        }
    }

    @Override
    public FfVodReadDto findbyid4PlayDto(Integer videoId, Integer playerIndex, Integer urlIndex) {
        FfVodWithBLOBs ffVod = ffVodMapper.findAllById(videoId);
        if(ffVod == null){

            return null;
        }else{
            FfVodReadDto dto = this.findbyid4dto(ffVod,playerIndex,urlIndex);
            return dto;
        }
    }

    @Override
    public List<IndexNavVodDto> findIndex(int navVodLength) {

        List<FfNavVO> navAvailList =  ffNavService.findAvailNavList();
        List<IndexNavVodDto> indexList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(navAvailList)){
                for(FfNavVO item : navAvailList){

                    if(item.getNavTitle().equals("更多")){
                        continue;
                    }

                    IndexNavVodDto indexNavVodDto = new IndexNavVodDto();
                    indexNavVodDto.setListId(item.getNavListId());
                    indexNavVodDto.setNavName(item.getNavTitle());

                    List<FfVod> vodlist =ffVodMapper.findAvailByListId(item.getNavListId(),navVodLength);

                    if(vodlist != null){
                        List<FfVodListDto> vodlist2 = new ArrayList<>();
                        for(FfVod item2 : vodlist){
                            FfVodListDto vodListDto = new FfVodListDto();
                            vodListDto.setPic(item2.getVodPic());
                            vodListDto.setVodId(item2.getVodId());
                            if(StringUtils.isNotEmpty(item2.getVodTitle())){
                                vodListDto.setTitle(item2.getVodTitle());
                                vodListDto.setTip(item2.getVodTitle());
                            }else{
                                vodListDto.setTitle(item2.getVodName());
                                vodListDto.setTip(item2.getVodName());
                            }
                            vodlist2.add(vodListDto);
                        }

                        indexNavVodDto.setVodlist(vodlist2);
                    }

                    indexList.add(indexNavVodDto);
                }
        }
        return indexList;
    }

    @Override
    public PageDto<FfVodListDto> dosearch(int pageNo, int pageSize, String key) {
        PageHelper.startPage(pageNo, pageSize, true);
        List<FfVod>  list = ffVodMapper.dosearch(key);
        PageInfo page = new PageInfo(list);
        if(list != null && list.size() >= 1){
            List<FfVodListDto> repList = new ArrayList<>();
            for(FfVod vod : list){
                FfVodListDto dto = transFfVod2FfVodListDto(vod);
                dto.setVodId(vod.getVodId());
                repList.add(dto);
            }
            page.setList(repList);
        }
        return   PageDto.transPageInfo(page);


    }

    public FfVodReadDto findbyid4dto(FfVodWithBLOBs ffVod,Integer playerIndex,Integer urlIndex) {
        FfVodReadDto bean = new FfVodReadDto();
        BeanUtils.copyProperties(ffVod,bean);
        List<VodReadPlayerInfo> playerList = new ArrayList<>();
        String vodPlay = bean.getVodPlay();
        List<FfPlayer> playerTmpList = new ArrayList<>();
        if(StringUtils.isNotEmpty(vodPlay)){
            String[] palyArray = vodPlay.split("\\$\\$\\$");
            for(String key : palyArray){
                FfPlayer player = ffPlayerService.findByEnName(key);
                if(player != null){
                    playerTmpList.add(player);
                }
            }

        }
        String vodPlayUrl = bean.getVodUrl();
        String playerName = null;
        String urlIndexName = null;
        String urlIndexUrl = null;

        if(StringUtils.isNotEmpty(vodPlayUrl)){
            String[] vodPlayUrlArray = vodPlayUrl.split("\\$\\$\\$");

            String tmp = null;
            String vodPlayUrlItem = null;
            for(int i = 0; i < vodPlayUrlArray.length; i++ ){
                vodPlayUrlItem = vodPlayUrlArray[i];

                VodReadPlayerInfo vodReadPlayerInfo = new VodReadPlayerInfo();
                vodReadPlayerInfo.setPlayerIndex(i);
                if(i < playerTmpList.size()){
                    FfPlayer player = playerTmpList.get(i);
                    vodReadPlayerInfo.setPlayerName(player.getPlayerNameZh());
                    if(playerIndex != null && playerIndex == i){
                        playerName =  player.getPlayerNameZh();
                    }
                }else{
                    vodReadPlayerInfo.setPlayerName("默认播放");
                    if(playerIndex != null && StringUtils.isEmpty(playerName)){
                        playerName =  "默认播放";
                    }
                }
                vodPlayUrlItem = vodPlayUrlItem.replaceAll("\r\n","{cms}");
                vodPlayUrlItem = vodPlayUrlItem.replaceAll("\r","{cms}");
                vodPlayUrlItem = vodPlayUrlItem.replaceAll("\n","{cms}");

                String[] urlArray = vodPlayUrlItem.split("\\{cms}");

                List<VodReadPlayerInfoChild> urlList = new ArrayList<>();
                for(int m = 0; m < urlArray.length ; m++){
                    tmp  = urlArray[m];
                    String[] urlRow =  tmp.split("\\$");


                    VodReadPlayerInfoChild vodReadPlayerInfoChild = new VodReadPlayerInfoChild();
                    if(urlRow.length == 1){
                        vodReadPlayerInfoChild.setShwoName(urlRow[0]);

                    }else if(urlRow.length == 2){
                        vodReadPlayerInfoChild.setShwoName(urlRow[0]);
                    }
                    if(playerIndex != null && playerIndex == i){
                        if(urlIndex != null  && urlIndex == m){
                            urlIndexName = urlRow[0];

                            if(urlRow.length == 0){

                                urlIndexUrl = urlRow[1];

                            }else if(urlRow.length == 1){
                                urlIndexUrl = urlRow[1];
                            }
                        }

                    }


                    vodReadPlayerInfoChild.setUrlindex(m );

                    urlList.add(vodReadPlayerInfoChild);
                }
                vodReadPlayerInfo.setUrlList(urlList);

                playerList.add(vodReadPlayerInfo);

            }

        }
        bean.setPlayerList(playerList);


        String vodType = bean.getVodType();
        List<String> vodTypeList = new ArrayList<>();
        if(StringUtils.isNotEmpty(vodType)){
            String[] types = vodType.split(",");
            vodTypeList =  Arrays.asList(types);
        }
        bean.setVodTypeList(vodTypeList);

        String vodArea = bean.getVodArea();
        List<String> vodAreaList = new ArrayList<>();
        if(StringUtils.isNotEmpty(vodArea)){
            String[] area = vodArea.split(",");
            vodAreaList =  Arrays.asList(area);
        }
        bean.setVodAreaList(vodAreaList);

        String vodActor = bean.getVodActor();
        List<String> vodActorList = new ArrayList<>();
        if(StringUtils.isNotEmpty(vodActor)){
            String[] vodActorArray = vodActor.split(",");
            vodActorList =  Arrays.asList(vodActorArray);
        }
        bean.setVodActorList(vodActorList);

        String vodDirector = bean.getVodDirector();
        List<String> vodDirectorList = new ArrayList<>();
        if(StringUtils.isNotEmpty(vodDirector)){
            String[] vodDirectorArray = vodDirector.split(",");
            vodDirectorList =  Arrays.asList(vodDirectorArray);
        }
        bean.setVodDirectorList(vodDirectorList);


        String vodTag = bean.getVodKeywords();
        List<String> vodTagList = new ArrayList<>();
        if(StringUtils.isNotEmpty(vodTag)){
            String[] vodTagArray = vodTag.split(",");
            vodTagList =  Arrays.asList(vodTagArray);
        }
        bean.setVodKeywordsList(vodTagList);
        bean.setPlayerName(playerName);
        bean.setUrlIndexName(urlIndexName);
        bean.setUrlIndexUrl(urlIndexUrl);


        bean.setVodActor(null);
        bean.setVodArea(null);
        bean.setVodDirector(null);
        bean.setVodUrl(null);
        bean.setVodType(null);
        bean.setVodPlay(null);
        bean.setVodKeywords(null);

        if(playerIndex != null){
            bean.setPlayerList(null);
        }
        if(bean.getVodAddtime() != null){
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            String vodAddtimeString = format.format(new Date(bean.getVodAddtime()*1000));
            bean.setVodAddtimeString(vodAddtimeString);
        }
        return bean;
    }


    private FfVodListDto transFfVod2FfVodListDto(FfVod vod){
        FfVodListDto dto = new FfVodListDto();
        if(StringUtils.isNotEmpty(vod.getVodTitle())){
            dto.setTip(vod.getVodTitle());
            dto.setTitle(vod.getVodTitle());
        }else{
            dto.setTip(vod.getVodName());
            dto.setTitle(vod.getVodName());
        }
        dto.setPic(vod.getVodPic());
        dto.setVodId(vod.getVodId());

        return dto;
    }
}
